﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using AppBox.BLL;
using FineUI;
using Newtonsoft.Json.Linq;
using ZHDJ.Core;
using ZHDJ.Data;
using ZHDJ.Data.DataCaching;
using Menu = ZHDJ.Data.Models.Box.Menu;
namespace ZHDJ.Web.App
{
    public partial class BranchMain : MainPage
    {
        /// <summary>
        /// 1后台管理 2 组织中心 3个人中心 4;注册用户
        /// </summary>
        public override int PageFlag
        {
            get
            {
                return 2;
            }
        }
        #region Page_Init
        protected void Page_Init(object sender, EventArgs e)
        {
            if (CurrentUser.runSate != "0")
            {
                //Response.Redirect("MassMain.aspx");
                //Response.Redirect(WebHost + "/RedirectTo.aspx?ToType=MassMain");
                RedirectTo("MassMain", "~/app/MassMain.aspx");
            }
            var tempbranchId = "";
            if (!PartyOrg.IsBranchAdmin(CurrentUser.LoginID, BranchID,out tempbranchId, false))
            {
                Response.Write(CHECK_POWER_FAIL_PAGE_MESSAGE);
                Response.End();
                //var dt = PartyOrg.GetBranchManageByLoginId(CurrentUser.LoginID);
                //if (dt.Rows.Count < 1)
                //{
                //    Response.Write(CHECK_POWER_FAIL_PAGE_MESSAGE);
                //    Response.End();
                //}
                //Response.Redirect("BranchMain.aspx?BranchID="+ dt.Rows[0]["BranchID"]);

            }
            if (tempbranchId !=  BranchID)
            {
                Response.Redirect(WebHost + "/app/BranchMain.aspx?BranchID=" + tempbranchId);

            }
            // 工具栏上的帮助菜单
            ToolHelp(btnHelp);
            // 用户可见的菜单列表
            List<Menu> menus = ResolveUserMenuList();
            if (menus.Count == 0)
            {
                Response.Write("系统管理员尚未给你配置菜单！");
                Response.End();
                return;
            }
            // 注册客户端脚本，服务器端控件ID和客户端ID的映射关系
            JObject ids = GetClientIDS(regionPanel, regionTop, mainTabStrip, txtUser,
                txtOnlineUserCount, txtCurrentTime, btnRefresh);
            RegisterMainJs(ids, menus, regionLeft);
        }

        private JObject GetClientIDS(params ControlBase[] ctrls)
        {
            JObject jo = new JObject();
            foreach (ControlBase ctrl in ctrls)
            {
                jo.Add(ctrl.ID, ctrl.ClientID);
            }
            jo.Add("pageFlag", PageFlag);
            var branch = DB.PartyOrgBranchs.Find(BranchID);
            jo.Add("branchName", branch.BranchName);
            return jo;
        }
        #endregion

        #region Page_Load

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadData();
            }
        }

        private void LoadData()
        {
            LoadChangeRole(btnChangeRole, ToolbarSeparator33);
            
        }
        #endregion

        #region Events

        protected void btnExit_Click(object sender, EventArgs e)
        {
            Exit();
        }

        public override List<Menu> ResolveUserMenuList()
        {
            // 当前登陆用户的权限列表
            List<string> rolePowerNames = GetRolePowerNames();
            // 当前用户所属角色可用的菜单列表
            List<Menu> menus = new List<Menu>();
            foreach (Menu menu in MenuHelper.MenusByID(SystemParameterCaching.Get(SystemParameterKey.BranchMainMenuID),2))
            {
                //
                var zhudianPower = new List<string>() { "GVisitRecordView", "GMassesVoiceView", "GrassrootsGroupView" };
                if (menu.ViewPower != null && zhudianPower.Contains( menu.ViewPower.Name))
                {
                    var zhudiancount = DB.PartyGrassrootsGroupItems.Where(a => a.BranchID == BranchID);
                    if (!zhudiancount.Any())
                    {
                        continue;
                    }
                }
                // 如果此菜单不属于任何模块，或者此用户所属角色拥有对此模块的权限
                if (menu.ViewPower == null || rolePowerNames.Contains(menu.ViewPower.Name))
                {
                    menus.Add(menu);
                }
                //
               
                

            }
           
            return menus;
        }

        #endregion
    }
}
